package com.zlll.winner.controller;

import com.alibaba.fastjson.JSONObject;
import com.zlll.winner.business.model.lhtUser.LhtUser;
import com.zlll.winner.business.model.lhtUser.LhtUserAddress;
import com.zlll.winner.business.model.lhtUser.LhtUserIntegralDetail;
import com.zlll.winner.business.service.lhtUser.ILhtUserAddressService;
import com.zlll.winner.business.service.lhtUser.ILhtUserIntegralDetailService;
import com.zlll.winner.business.service.lhtUser.ILhtUserIntegralTaskService;
import com.zlll.winner.business.service.lhtUser.ILhtUserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

@RequestMapping("api/lhtUser")
@RestController
public class LhtUserApiController {

    @Reference(version = "${dubbo.consumer.version}")
    private ILhtUserService iLhtUserService;


    @Reference(version = "${dubbo.consumer.version}")
    private ILhtUserAddressService addressService;

    @Reference(version = "${dubbo.consumer.version}")
    private ILhtUserIntegralDetailService integralDetailService;

    @Reference(version = "${dubbo.consumer.version}")
    private ILhtUserIntegralTaskService integralTaskService;

    @GetMapping("test")
    public Object test() {
        return iLhtUserService.count();
    }

    /**
     * 手机号登录
     *
     * @param data
     * @return
     */
    @PostMapping("quickLogin")
    public Object quickLogin(@RequestBody JSONObject data) {
        String phone = data.getString("phone");
        Assert.notNull(phone, "请传入手机号");

        String nickname = data.getString("nickname");
        String headImg = data.getString("headImg");

        LhtUser user = iLhtUserService.getLhtUser(phone);
        if (null == user) {
            user = new LhtUser();
            user.setNickname(nickname);
            user.setPhone(phone);
            user.setHeadImg(headImg);
            return iLhtUserService.register(user);
        } else {
            return user;
        }
    }

    /**
     * 根据ID获取用户信息
     *
     * @param data
     * @return
     */
    @PostMapping("info")
    public Object info(@RequestBody JSONObject data) {
        String phone = data.getString("phone");
        Assert.notNull(phone, "请传入手机号");
        return iLhtUserService.getLhtUser(phone);
    }

    /**
     * 注册
     *
     * @param data
     * @return
     */
    @PostMapping("register")
    public Object register(@RequestBody JSONObject data) throws Exception {
        String phone = data.getString("phone");
        Assert.notNull(phone, "请传入手机号");

        String password = data.getString("password");
        Assert.notNull(password, "请传入密码");
        String nickname = data.getString("nickname");
        LhtUser user = iLhtUserService.getLhtUser(phone);
        if (user != null) {
            throw new Exception("该手机已注册！");
        }
        user = new LhtUser();
        user.setPhone(phone);
        user.setNickname(nickname);
        user.setPassword(password);
        return iLhtUserService.register(user);
    }

    /**
     * 密码登录
     *
     * @param data
     * @return
     */
    @PostMapping("login")
    public Object login(@RequestBody JSONObject data) throws Exception {
        String phone = data.getString("phone");
        Assert.notNull(phone, "请传入手机号");

        String password = data.getString("password");
        Assert.notNull(password, "请传入密码");

        LhtUser user = iLhtUserService.getLhtUser(phone);
        if (null == user) {
            throw new Exception("该手机未注册！");
        }
        Boolean result = iLhtUserService.validateUserPassword(user, password);
        if (!result) {
            throw new Exception("密码错误！");
        }
        return user;
    }

    /**
     * 修改密码
     *
     * @param data
     * @return
     * @throws Exception
     */
    @PostMapping("password")
    public Object password(@RequestBody JSONObject data) throws Exception {
        String phone = data.getString("phone");
        Assert.notNull(phone, "请传入手机号");

        String oldPassword = data.getString("oldPassword");
        Assert.notNull(oldPassword, "请传入旧密码密码");

        String newPassword = data.getString("newPassword");
        Assert.notNull(newPassword, "请传入新密码");

        LhtUser user = iLhtUserService.getLhtUser(phone);
        if (null == user) {
            throw new Exception("该手机未注册！");
        }

        Boolean result = iLhtUserService.validateUserPassword(user, oldPassword);
        if (!result) {
            throw new Exception("旧密码错误！");
        }

        iLhtUserService.changePassword(user, newPassword);

        return true;
    }

    /**
     * 修改昵称
     *
     * @param data
     * @return
     * @throws Exception
     */
    @PostMapping("update")
    public Object updateNickname(@RequestBody JSONObject data) throws Exception {
        String phone = data.getString("phone");
        Assert.notNull(phone, "请传入手机号");
        LhtUser user = iLhtUserService.getLhtUser(phone);
        if (null == user) {
            throw new Exception("该手机未注册！");
        }
        String nickname = data.getString("nickname");
        if (nickname != null) {
            user.setNickname(nickname);
        }

        String headImg = data.getString("headImg");

        if (headImg != null) {
            user.setHeadImg(headImg);
        }

        Integer sex = data.getInteger("sex");
        if (sex != null) {
            user.setSex(sex);
        }
        iLhtUserService.updateById(user);

        return user;
    }

    /**
     * 新增收货地址
     *
     * @param address
     * @return
     */
    @PostMapping("saveAddress")
    public Object saveAddress(@RequestBody LhtUserAddress address) {
        return addressService.saveLhtUserAddress(address);
    }

    /**
     * 修改收货地址
     *
     * @param address
     * @return
     */
    @PostMapping("updateAddress")
    public Object updateAddress(@RequestBody LhtUserAddress address) {
        return addressService.updateLhtUserAddress(address);
    }

    @PostMapping("getAddress")
    public Object getAddress(@RequestBody JSONObject object) {
        Long userId = object.getLong("userId");

        return addressService.getLhtUserAddressList(userId);
    }

    /**
     * 增加积分
     *
     * @param object
     * @return
     */
    @PostMapping("addIntegral")
    public Object addIntegral(@RequestBody JSONObject object) throws Exception{
        Integer type = object.getInteger("type");
        Assert.notNull(type, "请传入积分类型");
        Long userId = object.getLong("userId");
        Assert.notNull(userId, "请传入用户id");

        if (type.equals(1)) {
            //每日任务
            Long taskId = object.getLong("taskId");
            Assert.notNull(taskId, "请传入任务ID");

            Boolean isAdd = integralDetailService.isAdd(userId, taskId);
            if (isAdd) {
                LhtUserIntegralDetail detail = new LhtUserIntegralDetail();
                detail.setTaskId(taskId);
                detail.setUserId(userId);
                detail.setType(type);
                return integralDetailService.saveDailyTasks(detail);
            }
        } else {
            //收听任务
            Integer accumulateTime = object.getInteger("accumulateTime");

            LhtUserIntegralDetail detail = new LhtUserIntegralDetail();
            detail.setType(type);
            detail.setUserId(userId);
            detail.setAccumulateTime(accumulateTime);
            return integralDetailService.saveWatchTasks(detail);
        }
        return true;
    }


    @PostMapping("getTodayIntegrail")
    public Object getTodayIntegrail(@RequestBody JSONObject object){
        Long userId = object.getLong("userId");
        Assert.notNull(userId, "请传入用户id");
        
        return integralDetailService.getTodayIntegrail(userId);
    }


    public static void main(String[] args) {
        Integer a = 10;
        Integer b = 4;
        System.out.println(b%a);
        System.out.println(b/a);

    }
}
